package cn.IT.action;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.User;
import cn.IT.service.IUserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller()
@Scope("prototype")

public class UserAction {
    
	private User user;

	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private IUserService userService;

	
	//用户登录
	public String login(){
        User user = userService.login(getUser());
		if( user != null){
			resultData = "{\"result\": \"success\"" + "," + "\"user\":" + JSONObject.fromObject(user) + "}";
			System.out.println(resultData);
		}
		//判断公司名是否已存在
		else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"该用户名或密码错误!\""+ "}";
			System.out.println(resultData);
		}
		return "success";
	}
	
	//根据用户id获得用户
	public String getUserById(){
        User user = userService.getUserById(getUser().getUserId());
		if( user != null){
			resultData = "{\"result\": \"success\"" + "," + "\"user\":" + JSONObject.fromObject(user) + "}";
			System.out.println(resultData);
		}
		else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"获取用户出错！\""+ "}";
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取所有用户
	public String getAllUsers(){
		List<User> users = userService.getAllUsers();
		if( users!=null){
			resultData = "{\"result\": \"success\"" + "," + "\"users\":" + JSONArray.fromObject(users) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取所有用户(除了自己)
	public String getAllUserExceptSelf(){
		List<User> users = userService.getAllUserExceptSelf(user);
		if( users!=null){
			resultData = "{\"result\": \"success\"" + "," + "\"users\":" + JSONArray.fromObject(users) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取所有经理级别的用户(除了自己)
	public String getAllApproversExceptSelf(){
		List<User> users = userService.getAllApproversExceptSelf(user);
		if( users!=null){
			resultData = "{\"result\": \"success\"" + "," + "\"users\":" + JSONArray.fromObject(users) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取所有用户(按贡献值)
	public String getAllUserOrderByContribute(){
		List<User> users = userService.getAllUserOrderByContribute();
		if( users!=null){
			resultData = "{\"result\": \"success\"" + "," + "\"users\":" + JSONArray.fromObject(users) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
    

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

}
